angular.module('CheckTransfer')
    .controller('CheckTransfer.ResultCtrl', ['$scope', '$state', '$ionicHistory', 'User', 'Http', 'imanDialog', 'CheckTransfer.CheckService', 'Config',
        function($scope, $state, $ionicHistory, User, Http, imanDialog, checkService, Config) {
            $scope.data = {
                container: checkService.getContainer(),
                scanQualifiedArray: checkService.getScanQualifiedObj(),
                scanUnQualifiedArray: checkService.getScanUnQualifiedArray(),
                User: User.get(),
                department: checkService.getDepart(checkService.getChoice()).DemandDepartment,
                wareHouse: Config.get('wareHouse'),
                ValidBarcodeList: checkService.getValidBarcodeList()
            };

            $scope.confirm = function() {
                if (!$scope.data.container || !$scope.data.container._id) {
                    imanDialog.toastBottom('货架信息为空');
                    return;
                }

                if ($scope.data.ValidBarcodeList.length === 0) {
                    imanDialog.toastBottom('球拍合格数量为0,无法提交');
                    return;
                }

                if (!$scope.data.department || !$scope.data.department._id) {
                    imanDialog.toastBottom('部门信息为空');
                    return;
                }

                imanDialog.showLoading();
                Http.post({
                    url: '/api/Kanban/PIckUp',
                    data: {
                        Parameter: {
                            userId: $scope.data.User._id,
                            containerId: $scope.data.container._id,
                            wareHouse: Config.get('wareHouse'),
                            validBarcodeList: $scope.data.ValidBarcodeList,
                            departmentId: $scope.data.department._id
                        }
                    }
                }).then(function() {
                    imanDialog.toastCenter('提交成功');
                    checkService.setContainer();
                    $ionicHistory.goBack(-2);
                }).finally(function () {
                    imanDialog.hideLoading();
                });
            };
        }
    ]);
